﻿''' <summary>
''' This class implements a windows services collector.
''' </summary>
''' <remarks>
''' The purpose of the windows services collector is to retrieve information
''' about configured windows services and output it to the system info report.
''' </remarks>
Public Class WinServicesCollector
    Implements ICollector

    ''' <summary>
    ''' List of writers passed when Startup() is called.
    ''' </summary>
    ''' <remarks></remarks>
    Protected m_writers As List(Of Writer.IWriter)

    Public Sub Cleanup() Implements ICollector.Cleanup

        'Nothing to do

    End Sub

    Public Sub Run() Implements ICollector.Run

        'Write our category name.
        Writer.WritersHelper.WriteCategory("Windows Services Information", m_writers)
        Writer.WritersHelper.WriteSection("Services", m_writers)

        Try
            'Get all services.
            Dim srv() As ServiceProcess.ServiceController = System.ServiceProcess.ServiceController.GetServices()
            'Process all services.
            For i As Integer = 0 To srv.GetUpperBound(0)
                Try
                    Writer.WritersHelper.WriteEntry("Friendly name", srv(i).DisplayName, m_writers)
                    Writer.WritersHelper.WriteEntry("Service name", srv(i).ServiceName, m_writers)
                    Writer.WritersHelper.WriteEntry("Status", srv(i).Status.ToString(), m_writers)
                    Writer.WritersHelper.WriteEntry("Type", srv(i).ServiceType.ToString(), m_writers)
                Catch ex As Exception
                    'Error retrieving information for service.
                    Writer.WritersHelper.WriteEntryError("Error retrieving information for service (" + ex.Message() + ")", m_writers)
                End Try
                Writer.WritersHelper.WriteBlankEntry(m_writers)
            Next
        Catch ex As Exception
            'Error retrievign information for services.
            Writer.WritersHelper.WriteSectionError("Error retrieving information for services (" + ex.Message() + ")", m_writers)
        End Try

    End Sub

    Public Sub Startup(ByVal cfgFile As String, ByVal writers As System.Collections.Generic.List(Of Writer.IWriter)) Implements ICollector.Startup

        'Get all writers.
        m_writers = writers

    End Sub

End Class
